package com.black.app.service.impl.my;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.req.my.myorders.AppOrderAftersaleAddReq;
import com.black.app.domain.req.my.myorders.SysOrderAfterSaleQueryReq;
import com.black.app.domain.resp.my.myorders.AppOrderAfterSaleResp;
import com.black.app.domain.resp.my.myorders.AppOrderAftersaleDetailsResp;
import com.black.app.domain.resp.my.myorders.SysAfterSaleResp;
import com.black.app.domain.store.AppStoreOrders;
import com.black.app.service.store.AppStoreOrdersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.black.app.domain.my.AppMyOrderAftersale;
import com.black.app.service.my.AppMyOrderAftersaleService;
import com.black.app.mapper.my.AppMyOrderAftersaleMapper;

import java.util.Date;
import java.util.Objects;

/**
 * @author admin
 * @description 针对表【app_my_order_aftersale(订单售后表)】的数据库操作Service实现
 * @createDate 2024-11-01 15:23:40
 */
@Service
public class AppMyOrderAftersaleServiceImpl extends ServiceImpl<AppMyOrderAftersaleMapper, AppMyOrderAftersale>
        implements AppMyOrderAftersaleService {

    @Autowired
    private AppStoreOrdersService appStoreOrdersService;

    /**
     * 后台分页查询售后订单
     *
     * @param req
     * @return
     */
    @Override
    public PageInfo<SysAfterSaleResp> paginationQuery(SysOrderAfterSaleQueryReq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.paginationQuery(req);
        });
    }

    /**
     * 用户售后订单列表
     *
     * @param userId
     * @return
     */
    @Override
    public PageInfo<AppOrderAfterSaleResp> aftersaleList(Integer userId) {
        return PageHelper.startPage(1, 10).doSelectPageInfo(()->{
            this.baseMapper.aftersaleList(userId);
        });
    }

    /**
     * 根据售后订单id查询详情
     *
     * @param id
     * @return
     */
    @Override
    public AppOrderAftersaleDetailsResp finById(Integer id) {
        return this.baseMapper.findOrderAftersaleDetailsById(id);
    }

    /**
     * 新增售后订单
     *
     * @param req
     * @return
     */
    @Override
    public boolean addOrderAftersale(AppOrderAftersaleAddReq req) {
        //判断订单售后是否已经存在
        if (this.baseMapper.exists(new LambdaQueryWrapper<AppMyOrderAftersale>().eq(AppMyOrderAftersale::getOrderId, req.getOrderId()))) {
            throw new RuntimeException("订单已申请售后");
        }
        //判断订单是否存在
        if (!this.appStoreOrdersService.exists(new LambdaQueryWrapper<AppStoreOrders>().eq(AppStoreOrders::getOrderId, req.getOrderId()))) {
            throw new RuntimeException("订单不存在");
        }
        //根据订单id获取订单实例
        AppStoreOrders order = this.appStoreOrdersService.getById(req.getOrderId());
        //将订单状态更改为售后状态
        order.setOrderStatus(4);
        this.appStoreOrdersService.updateById(order);
        //新增售后订单
        AppMyOrderAftersale aftersale = new AppMyOrderAftersale();
        if (Objects.nonNull(req.getId())){
            aftersale.setId(req.getId());
        }
        aftersale.setOrderId(req.getOrderId());
        aftersale.setCode(req.getCode());
        aftersale.setStatus(1);
        aftersale.setWay(req.getWay());
        aftersale.setApplyTime(new Date());
        aftersale.setCause(req.getCause());
        //保存新增
        return this.save(aftersale);
    }
}




